/* 
 * Song Exporter
 *
 * Choi Jonghwan<saxboy@saxboy.pe.kr>
 * 2006/04/23
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "songrenderer.h"


SONGRENDERER songrenderer_New(void)
{
	SONGRENDERER sr=NULL; 
	sr = (SONGRENDERER)malloc(sizeof(_SONGRENDERER)); 
	if (sr!=NULL)
	{
		memset(sr, 0, sizeof(_SONGRENDERER)); 
		return sr; 
	}

	songrenderer_Delete(sr); 
	return NULL; 
}

int songrenderer_Delete(SONGRENDERER sr)
{
	if (sr==NULL) return -1; 
	free(sr); 
	return 1; 
}

int songrenderer_SetSong(SONGRENDERER sr, SONG song)
{
	if (sr==NULL) return -1; 
	if (song==NULL) return -1; 

	sr->song = song; 
	return 1; 
}

int songrenderer_SetStyle(SONGRENDERER sr, SONGSTYLE style)
{
	if (sr==NULL) return -1; 
	if (style==NULL) return -1; 

	sr->style = style; 

	return 1; 
}

int songrenderer_Run(SONGRENDERER sr)
{
	if (sr==NULL) return -1; 
	if (sr->song==NULL) return -1; 
	if (sr->style==NULL) return -1; 

	/* let each artist play his own song. 
	 *
	 * FIXME: any good idea for jams?
	 */
	{
		SONGSTYLE style = sr->style; 
		SONG song = sr->song; 
		int i=0; 

		for (i=0; i< songstyle_GetArtistCount(style); i++)
		{
			ARTIST artist = songstyle_GetArtist(style, i); 

			if ( artist==NULL) continue; 

			/* add new voice for each artist */
			{
				VOICE voice = NULL; 
				
				if ( (voice=voice_New())==NULL) 
					goto error; 
				if ( artist_RenderSongToVoice(artist, song, voice) < 0 ) 
					goto error; 
				if ( song_AddVoice(song, voice) < 0 ) 
					goto error; 
			}
		}
	}

	return 1; 

error:
	return -1; 
}



